package com.ruipengkj.view.orderView;
/**
 * 说明：礼品管理
 * 
 * @Author：孙航
 */
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.table.TableRowSorter;

import com.ruipengkj.ApplicationContext;
import com.ruipengkj.bean.TOrderRefund;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.page.PageDelegale;
import com.ruipengkj.model.OrderRefundModel;
import com.ruipengkj.service.TOrderRefundService;
import com.ruipengkj.view.base.BasePagePanel;

public class OrderRefundPanel extends BasePagePanel<TOrderRefund> implements PageDelegale ,Runnable{

	private static final long serialVersionUID = 1L;
	
	private JButton queryButton; //查询
//	private JButton addButton; //增加
	private JButton updateButton; //修改
//	private JButton delButton; //删除
	
	private JTable table;
	private JScrollPane tableJsp;
	private OrderRefundModel orderRefundModel;
	
//	private PagingComponent pagingComponent;//翻页组件
//	private PageView<TOrderRefund> pageView;//翻页主键
	private TOrderRefundService tOrderRefundService;
	private OrderRefundDialog orderRefundDialog;
	
	public OrderRefundPanel() {
		setLayout(new BorderLayout());
		init();
		initListener();
		tOrderRefundService=BuildSpringApplication.getInstance().getTOrderRefundServiceImpl();
	}
	
	private JPanel initTop() {
		JPanel top_panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		
		queryButton = new JButton("查询");
		
		top_panel.add(queryButton);
		return top_panel;
	}
	
	private void init() {
//		pageView = new PageView<TOrderRefund>();
		orderRefundModel = new OrderRefundModel();
		table=uiFactory.createJTable(orderRefundModel, null, this, 30, JLabel.CENTER);
		tableJsp=new JScrollPane(table);
//		pagingComponent=new PagingComponent();
//		pagingComponent.setPageDelegale(this);
		
		JPanel gardPanel=new JPanel(new GridLayout(2,1));
		JPanel jp1=new JPanel(new FlowLayout(FlowLayout.RIGHT));
//		addButton = new JButton("添加礼品信息(Ctrl+A)");
		updateButton = new JButton("处理退款信息(Ctrl+M)");
//		delButton = new JButton("删除礼品信息(Ctrl+D)");
//		jp1.add(addButton);
		jp1.add(updateButton);
//		jp1.add(delButton);
//		gardPanel.add(pagingComponent);
		gardPanel.add(jp1);
		this.setButtonEnabled(false);
		
		this.add(initTop(),BorderLayout.NORTH);
		this.add(tableJsp,BorderLayout.CENTER);
		this.add(gardPanel,BorderLayout.SOUTH);
		//排序
		TableRowSorter<OrderRefundModel> tableRowSorter = new TableRowSorter<OrderRefundModel>(orderRefundModel);  
		table.setRowSorter(tableRowSorter);
		this.termQuery(1);
	}
	
	//初始化监听器
	private void initListener() {
		queryButton.addActionListener(this);
//		addButton.addActionListener(this);
//		addButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
		updateButton.addActionListener(this);
		updateButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_M, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
//		delButton.addActionListener(this);
//		delButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_D, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
	}
	private void setButtonEnabled(boolean flag){
		updateButton.setEnabled(flag);
//		delButton.setEnabled(flag);
	}
	@Override
	public void run() {
		this.termQuery(1);
	}
	
	//条件查询
	@Override
	protected void termQuery(final int currentPage){
		new Thread(){
			public void run(){
				tableJsp.getViewport().remove(table);
				tableJsp.getViewport().add(waitComponent);
				try {
					List<TOrderRefund> allList = new ArrayList<TOrderRefund>();
					List<TOrderRefund> list = new ArrayList<TOrderRefund>();
					TOrderRefund torderrefund = new TOrderRefund();
					allList = tOrderRefundService.findService(torderrefund);
					for(int i = 0; i < allList.size(); i++){
						torderrefund = allList.get(i);
						if(torderrefund.getTOrder().getSaler().equals(ApplicationContext.EMPLOYEE.getEmployeeID())){
							list.add(torderrefund);
						}
					}
					if(list!=null){
						Util.clearTableRowSorter(table);
						orderRefundModel.setRowData(list);
						SwingUtilities.invokeLater(new Runnable() {
							@Override
							public void run() {
								tableJsp.getViewport().remove(waitComponent);
								tableJsp.getViewport().add(table);
								Util.setJTabelAutoClumnWidth(table);
//								pagingComponent.show();
							}
						});
					}else{
						//没有订单数据
						waitComponent.setText("不存在数据");
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}
//	@Override
//	protected PageView<TOrderRefund> getPageView() {
//		return this.pageView;
//	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==queryButton){
			//查询按钮
			this.termQuery(1);
		}
//		else if(e.getSource()==addButton){
//			//添加按钮
//			orderRefundDialog = new OrderRefundDialog(null,"添加礼品",true);
//			orderRefundDialog.showDialog(null);
//			if(orderRefundDialog.isSuccess){
//				this.termQuery(1);
//			}
//		}else 
		if(e.getSource()==updateButton){
			//处理退款信息按钮
			this.modifyStore();
		}
//			else if(e.getSource()==delButton){
//			//删除按钮
//			this.deleteGift();
//		}
	}
//	@Override
//	public void keyReleased(KeyEvent e) {
//		if(e.getSource()==table){
//			if(e.getKeyCode()==KeyEvent.VK_DELETE){
//				//删除按钮
//				this.deleteGift();
//			}
//		}
////		else if(e.getSource()==storageID.getText()||e.getSource()==storageName.getText()){
////			//查询
////			if(e.getKeyCode()==KeyEvent.VK_ENTER){
////				queryButton.doClick();
////			}
////		}
//	}
	@Override
	public void mouseClicked(MouseEvent e) {
		if(e.getSource()==table){
			this.setButtonEnabled(true);
			if(table.getSelectedRowCount()!=0){
				if(e.getClickCount() == 2){
	    			this.modifyStore();
	    		}
			}
		}
	}
	//修改退款记录
	private void modifyStore(){
		if(table.getRowCount()!=0){
			int row=table.getSelectedRow();
			if(row!=-1){
				String refundID=(String)table.getValueAt(row, 0);
				try {
					TOrderRefund tOrderRefund=tOrderRefundService.getByIdService(refundID);
					orderRefundDialog = new OrderRefundDialog(null,"修改退款",true);
					orderRefundDialog.showDialog(tOrderRefund);
					if(orderRefundDialog.isSuccess){
						this.termQuery(1);
					}
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}else{
				JOptionPane.showMessageDialog(null,"请选择行在进行修改");
				return;
			}
		}
	}
	//删除
//	private void deleteGift(){
//		if(table.getRowCount()!=0){
//			int row=table.getSelectedRow();
//			if(row!=-1){
//				if(JOptionPane.showConfirmDialog(null, "是否要删除选择记录")==0){
//					String storeID=(String)table.getValueAt(row, 0);
//					try {
//						giftService.deleteService(storeID);
//						JOptionPane.showMessageDialog(null, "删除成功");
//						this.termQuery(1);
//					} catch (Exception e1) {
//						JOptionPane.showMessageDialog(null, "删除失败");
//						e1.printStackTrace();
//					}
//				}
//			}else{
//				JOptionPane.showMessageDialog(null,"请选择行在进行删除");
//				return;
//			}
//		}
//	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}
	
}
